package com.china08.yxyapi.api.manage.edu.platform.live.lesson.waiting;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.live.YxyLiveCatalog;
import com.china08.yxyapi.entity.db.live.YxyLiveLesson;
import com.china08.yxyapi.entity.db.live.YxyLiveLessonItem;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.live.YxyLiveCatalogRepository;
import com.china08.yxyapi.repository.db.live.YxyLiveLessonItemRepository;
import com.china08.yxyapi.repository.db.live.YxyLiveLessonRepository;
import com.china08.yxyapi.util.DateToolUtils;

@CrossOrigin
@RestController
public class LiveLessonWaitingDetailController extends YxyApiBaseController {

    @Autowired
    private YxyLiveLessonRepository yxyLiveLessonRepository;

    @Autowired
    private YxyLiveLessonItemRepository yxyLiveLessonItemRepository;

    @Autowired
    private YxyLiveCatalogRepository catalogRepository;

    @RequestMapping(value = "/api_yw/manage/edu/platform/live/lesson/waiting/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String id = reqModel.id;

        YxyLiveLesson yxyLiveLesson = yxyLiveLessonRepository.findById(id);

        RespModel model = new RespModel();
        String cId1 = yxyLiveLesson.getCatalogId1();
        String cId2 = yxyLiveLesson.getCatalogId2();
        String cId3 = yxyLiveLesson.getCatalogId3();

        if (StringUtils.isNotBlank(cId1)) {
            YxyLiveCatalog findById = catalogRepository.findById(cId1);
            model.catalog1Id = cId1;
            model.catalog1Name = findById.getName();
        }
        if (StringUtils.isNotBlank(cId2)) {
            YxyLiveCatalog findById = catalogRepository.findById(cId2);
            model.catalog2Id = cId2;
            model.catalog2Name = findById.getName();
        }
        if (StringUtils.isNotBlank(cId3)) {
            YxyLiveCatalog findById = catalogRepository.findById(cId3);
            model.catalog3Id = cId3;
            model.catalog3Name = findById.getName();
        }

        model.count = yxyLiveLesson.getCount();
        model.createDate = DateToolUtils.format(yxyLiveLesson.getCreateDate(), "yyyy-MM-dd");
        model.name = yxyLiveLesson.getName();
        model.liveType = yxyLiveLesson.getLiveType() == 0 ? "直播+录播" : "直播";
        model.schoolName = yxyLiveLesson.getTeacherSchoolNick();
        model.status = "待审核";
        model.user = yxyLiveLesson.getTeacherNick();
        model.userId = yxyLiveLesson.getTeacherId();
        model.cover = yxyLiveLesson.getCover();
        model.id = yxyLiveLesson.getId();
        model.summary = yxyLiveLesson.getSummary();
        List<YxyLiveLessonItem> items = yxyLiveLessonItemRepository.findByLessonId(id);
        List<Pojo> iList = new ArrayList<Pojo>();
        int i = 1;
        for (YxyLiveLessonItem yxyLiveLessonItem : items) {
            Pojo pojo = new Pojo();
            pojo.bTime = DateToolUtils.format(yxyLiveLessonItem.getBeginDate(), DateToolUtils.FORMAT_LONG);
            pojo.eTime = DateToolUtils.format(yxyLiveLessonItem.getEndDate(), DateToolUtils.FORMAT_LONG);
            pojo.id = yxyLiveLessonItem.getId();
            pojo.name = "课时" + i;
            iList.add(pojo);
            i++;
        }
        model.items = iList;

        return model;
    }

    public static class Pojo {
        public String id;
        public String name;
        public String bTime;
        public String eTime;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String id;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(id), "课程ID为空");
        }
    }

    public static class RespModel extends YxyApiRespModel {

        public String id;
        public String createDate;
        public String name;
        public String status;
        public Integer count;
        public String cover;
        public String catalog1Name;
        public String catalog1Id;
        public String catalog2Name;
        public String catalog2Id;
        public String catalog3Name;
        public String catalog3Id;
        public String user;
        public String userId;
        public String schoolName;
        public String liveType;
        public List<Pojo> items;
        public String summary;
    }

}
